package com.myph.phctrl_t;
import java.util.Iterator;

import com.example.phctrl_t.R;
import com.myph.chart.IDemoChart;
import com.myph.chart.ShowChart;
import com.myph.model.ClientSocketNew;
import com.myph.model.Util;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.AvoidXfermode.Mode;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SetGapTimeActivity extends Activity implements OnClickListener {

		
		private Button btn_submit;
		private EditText edt_gap_time;
	
		private String ip, port;
		private String on_or_off;
		private StringBuilder response = new StringBuilder();

		private int gap_time = -1;
		private long prevClickTime = 0;
	    
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_set_gap_time);

	        ip = Util.getSharedPref(this, "ip");
	        port = Util.getSharedPref(this, "port");

	        edt_gap_time = (EditText) findViewById(R.id.edt_gap_time);
	        btn_submit = (Button) findViewById(R.id.btn_submit);
	        btn_submit.setOnClickListener(this);
	       
	    }

	    @SuppressLint("HandlerLeak")
	    Handler handler = new Handler() {
	        public void handleMessage(android.os.Message msg) {
	            switch (msg.what) {
	            case 1://$phfre,n(n*30s)#
	            	String num = null;
	                
	                //数据在输入中已经限定位数,只需检测不能为0
	                if(edt_gap_time.getText() == null || edt_gap_time.getText().toString().trim().equals("")){
	                	Util.myToast(getApplicationContext(), "未输入测量时间间隔");
	                	return;
	                }else{
	                	num =  edt_gap_time.getText().toString();
	                	if(Integer.parseInt(num) == 0){
	                		Util.myToast(getApplicationContext(), "测量时间不能为0");
	                		return;
	                	}
	                }
	                gap_time = Integer.parseInt(num);//*30在MCU中完成
	                String res = "$phfre," + String.valueOf(gap_time) + "#";
	                ClientSocketNew csn = new ClientSocketNew(ip, port, handler);
	                csn.setNewRequest(res, response);
	                csn.start();
	                break;
	            case 1002:
	                Toast.makeText(getApplicationContext(), "fail to connect",
	                        Toast.LENGTH_SHORT).show();
	                finish();
	                break;
	            case 1003:
	                if (response != null) {
	                	String resString = response.toString().trim();
	                	if(resString.equals("1")){
	                    	Util.myToast(getApplicationContext(), "设置成功~");
	                    	Util.saveSharedPref(getApplicationContext(), "gap_time", String.valueOf(gap_time));
	                    }
	                    if (resString.equals("")) {
	                    	Toast.makeText(getApplicationContext(), "异常", Toast.LENGTH_SHORT).show();
	                    }
	                }
	                finish();
	                break;
	            case 1004:
	                Toast.makeText(getApplicationContext(), "连接超时,操作失败",
	                        Toast.LENGTH_SHORT).show();
	                finish();
	                break;
	            default:
	                Toast.makeText(getApplicationContext(), "handler error!",
	                        Toast.LENGTH_LONG).show();
	                break;
	            }

	        }
	    };

	    public void sendMsg(int what, Handler handler) {
	        Message msg = new Message();
	        msg.what = what;
	        handler.sendMessage(msg);
	    }

	    @Override
	    public void onClick(View v) {
	    	long pressGap = System.currentTimeMillis() - prevClickTime ;
			if(pressGap < 1000){
				return;
			}else{
				prevClickTime = System.currentTimeMillis();
			}
	        switch (v.getId()) {
	        case R.id.btn_submit:
	            sendMsg(1, handler);
	            break;
	        default:
	            break;
	        }
	    }
	
}
